﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;

namespace smx
{
    public partial class supplierEdit : Form
    {
        //for converting the id in the edit version 0 for new records
        private int id = 0;
        //normal constructor
        public supplierEdit()
        {
            InitializeComponent();
        }
       
        //----------------------------------------------------------------------------------------------------------------------------
        //constructor for the edit form version
        //----------------------------------------------------------------------------------------------------------------------------
        public supplierEdit(string suppid)
        {
            InitializeComponent();
            id = Convert.ToInt32(suppid);
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //the load event where we set the information for the edit form
        //----------------------------------------------------------------------------------------------------------------------------
        private void supplierEdit_Load(object sender, EventArgs e)
        {
            if (id != 0)
            {
                try
                {

                    stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                    var supper = from supp in data.suppliers
                                 where supp.id == id
                                 select supp;

                    foreach (supplier s in supper)
                    {
                        idTxt.Text = s.id.ToString();
                        firstnameTxt.Text = s.firstname;
                        lastnameTxt.Text = s.lastname;
                        emailTxt.Text = s.email;
                        phoneTxt.Text = s.phone;
                        lineoneTxt.Text = s.addone;
                        linetwoTxt.Text = s.addtwo;
                        townTxt.Text = s.town;
                        countyTxt.Text = s.county;
                        postcodeTxt.Text = s.postcode;
                        companyTxt.Text = s.company;
                        notesTxt.Text = s.notes;
                        suppsinceDate.Value = (DateTime)s.dob;
                        fullnameLbl.Text = s.lastname + " , " + s.firstname;
                    }
                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                       "There was an error!",
                       "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                       err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
               
            }
        }
        //----------------------------------------------------------------------------------------------------------------------------
        //cancel the save changes
        //----------------------------------------------------------------------------------------------------------------------------
        private void cancelBtn_Click(object sender, EventArgs e)
        {

            if (
            cTaskDialog.MessageBox(this,
                "Confirmation",
                "Are you sure you wish to cancel?",
                "Choosing yes will close this form and no data will be saved. Do you wish to proceed?",
                eTaskDialogButtons.YesNo,
                eSysIcons.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //save the changes and then close
        //----------------------------------------------------------------------------------------------------------------------------
        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (id == 0)
            {
                addnew();
            }
            else
            {
                saveChanges();
            }

            MasterForm form = (MasterForm)Application.OpenForms["MasterForm"];
            form.getSuppliers();
            this.Close();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //if an add form
        //----------------------------------------------------------------------------------------------------------------------------
        private void addnew()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            try
            {
                supplier s = new supplier();
                s.firstname = firstnameTxt.Text;
                s.lastname = lastnameTxt.Text;
                s.email = emailTxt.Text;
                s.phone = phoneTxt.Text;
                s.addone = lineoneTxt.Text;
                s.addtwo = linetwoTxt.Text;
                s.town = townTxt.Text;
                s.county = countyTxt.Text;
                s.postcode = postcodeTxt.Text;
                s.company = companyTxt.Text;
                s.dob = suppsinceDate.Value;
                s.notes = notesTxt.Text;

                data.AddTosuppliers(s);

                data.SaveChanges();
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                                             "There was an error!",
                                             "There was an error in attempting to add the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                                             err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);


            }
            
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //if an edit form
        //----------------------------------------------------------------------------------------------------------------------------
        private void saveChanges()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            var q = from s in data.suppliers
                    where s.id == id
                    select s;
            try
            {
                foreach (supplier s in q)
                {
                    s.firstname = firstnameTxt.Text;
                    s.lastname = lastnameTxt.Text;
                    s.email = emailTxt.Text;
                    s.phone = phoneTxt.Text;
                    s.addone = lineoneTxt.Text;
                    s.addtwo = linetwoTxt.Text;
                    s.town = townTxt.Text;
                    s.county = countyTxt.Text;
                    s.postcode = postcodeTxt.Text;
                    s.company = companyTxt.Text;
                    s.dob = suppsinceDate.Value;
                    s.notes = notesTxt.Text;
                    data.SaveChanges();
                }
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                        "There was an error!",
                        "There was an error in attempting to edit the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                        err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
            
        }

    }
}
